package com.ueboot.weishop.admin.controller.order;

import com.google.common.collect.Lists;
import com.ueboot.core.http.response.Response;
import com.ueboot.core.utils.DateUtils;
import com.ueboot.weishop.admin.controller.order.vo.OrderInvoiceReq;
import com.ueboot.weishop.admin.controller.order.vo.OrderInvoiceResp;
import com.ueboot.weishop.entity.order.OrderExtensionEntity;
import com.ueboot.weishop.entity.order.OrderInvoiceEntity;
import com.ueboot.weishop.entity.order.OrderReceiverEntity;
import com.ueboot.weishop.service.order.OrderExtensionService;
import com.ueboot.weishop.service.order.OrderInvoiceNewService;
import com.ueboot.weishop.service.order.OrderInvoiceService;
import com.ueboot.weishop.service.order.OrderReceiverService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:fanjiapeng
 * @Description:
 * @Date:Create in 16:10 2017/11/8
 * @Modified By:
 */
@Slf4j
@RestController
@RequestMapping(value = "/orderInvoice")
public class OrderInvoiceController {

    @Autowired
    private OrderReceiverService orderReceiverService;

    @Autowired
    private OrderInvoiceService service;

    @Autowired
    private OrderInvoiceNewService orderInvoiceNewService;

    @RequiresPermissions("orderInvoice:read")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Response<Page<OrderInvoiceResp>> page(@PageableDefault(value = 15, sort = {"id"}, direction = Sort.Direction.DESC)
                                                         Pageable pageable, @RequestBody(required = false) OrderInvoiceReq request) {

        OrderInvoiceEntity orderInvoice = new OrderInvoiceEntity();
        BeanUtils.copyProperties(request, orderInvoice);
        Page<OrderInvoiceEntity> entities = orderInvoiceNewService.pagingSearch(pageable, orderInvoice, request.getOrderNo(), request.getStartDate(), request.getEndDate());

        Page<OrderInvoiceResp> body = entities.map(entity -> {
            OrderInvoiceResp resp = new OrderInvoiceResp();
            BeanUtils.copyProperties(entity, resp);
            if (null != entity.getMakeDate()) {
                resp.setMakeDate(DateUtils.format(entity.getMakeDate(), DateUtils.FORMAT_DATE_TIME_DEFAULT));
            }
            resp.setVin(entity.getOrder().getKeyForLimit());
            resp.setMobile(entity.getCustomer().getMobile());
            resp.setOrderNo(entity.getOrder().getOrderNo());
            OrderReceiverEntity orderReceiver = orderReceiverService.findByOrderOrderNo(entity.getOrder().getOrderNo());
            if (orderReceiver != null) {
                resp.setAreaFullName(orderReceiver.getProvinceName() + orderReceiver.getCityName() + orderReceiver.getAreaName());
                resp.setAddress(orderReceiver.getAddress());
                resp.setConsignee(orderReceiver.getConsignee());
                resp.setMobile(orderReceiver.getMobile());
            }
            if (orderInvoice.getCustomer() != null) {
                resp.setName(orderInvoice.getCustomer().getName());
                resp.setMobileAudit(orderInvoice.getCustomer().getMobile());
            }
            return resp;
        });
        return new Response<>(body);
    }


    @RequiresPermissions("orderInvoice:read")
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    public Response<List<OrderInvoiceResp>> find(@RequestBody(required = false) OrderInvoiceReq request) {

        OrderInvoiceEntity orderInvoice = new OrderInvoiceEntity();
        BeanUtils.copyProperties(request, orderInvoice);
        List<OrderInvoiceEntity> entities = orderInvoiceNewService.findSearch(orderInvoice, request.getOrderNo(), request.getStartDate(), request.getEndDate());

        List<OrderInvoiceResp> bodyResp = Lists.newLinkedList();
        entities.forEach(entity -> {
            OrderInvoiceResp resp = new OrderInvoiceResp();
            BeanUtils.copyProperties(entity, resp);
            if (null != entity.getMakeDate()) {
                resp.setMakeDate(DateUtils.format(entity.getMakeDate(), DateUtils.FORMAT_DATE_TIME_DEFAULT));
            }
            resp.setVin(entity.getOrder().getKeyForLimit());
            resp.setMobile(entity.getCustomer().getMobile());
            resp.setOrderNo(entity.getOrder().getOrderNo());
            bodyResp.add(resp);
        });
        return new Response<>(bodyResp);
    }

    @RequiresPermissions("orderInvoice:save")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Response<Void> save(@RequestBody OrderInvoiceReq req) {
        OrderInvoiceEntity entity = new OrderInvoiceEntity();
        if (req.getId() != null) {
            entity = service.get(req.getId());
        }
        BeanUtils.copyProperties(req, entity);
        service.save(entity);
        return new Response<>();
    }

    @RequiresPermissions("orderInvoice:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Response<Void> delete(Long[] id) {
        service.delete(id);
        return new Response<>();
    }

    @RequiresPermissions("orderInvoice:read")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Response<OrderInvoiceResp> get(@PathVariable Long id) {
        OrderInvoiceEntity entity = service.get(id);
        OrderInvoiceResp resp = new OrderInvoiceResp();
        BeanUtils.copyProperties(entity, resp);
        return new Response<>(resp);
    }


}
